package com.dwjm.shop.Wwj.Goods;

import com.dwjm.shop.Wwj.utils.ConnectionDatabaseForMySQLUseJdbc;
import javafx.scene.transform.Scale;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    Connection connection=null;
    PreparedStatement pstmt=null;
    ResultSet rset=null;
    Goods goods=null;

    public GoodsDaoImpl(){
        connection= ConnectionDatabaseForMySQLUseJdbc.getConnection();
    }
//    查询商品
    @Override
    public List<Goods> findGoodsByword(String keyword){
        List<Goods> list=new ArrayList<>();
        try{
            pstmt=connection.prepareStatement("select id,title,price,descr,img,state,mid from t_goods where title like ?");
            pstmt.setString(1,"%"+keyword+"%");
            rset=pstmt.executeQuery();
            Goods goods=null;
            while(rset.next()){
                goods=new Goods(
                        rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("descr"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid"));
                list.add(goods);
            }
            return list;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

//    编辑商品内容
    @Override
    public int updateGoods(Goods goods) {
        try {
            pstmt = connection.prepareStatement(
                    "UPDATE t_goods SET title=?, price=?, descr=?, img=?, mid=? WHERE id=?");
            pstmt.setString(1, goods.getTitle());
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImg());
            pstmt.setInt(5, goods.getMid());
            pstmt.setInt(6, goods.getId());

            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0 ? 1 : 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
//    查询单条记录
    @Override
    public Goods getDetail(int id) {
        try {
            pstmt = connection.prepareStatement(
                    "SELECT id, title, price, descr, img, state, mid FROM t_goods WHERE id=?");
            pstmt.setInt(1, id);
            rset = pstmt.executeQuery();

            if (rset.next()) {
                goods = new Goods();
                goods.setId(rset.getInt("id"));
                goods.setTitle(rset.getString("title"));
                goods.setPrice(rset.getBigDecimal("price"));
                goods.setDescr(rset.getString("descr"));
                goods.setImg(rset.getString("img"));
                goods.setState(rset.getInt("state"));
                goods.setMid(rset.getInt("mid"));
            }
            return goods;

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //    删除商品
    @Override
    public int deleteGoods(Integer id){
        try{
            pstmt = connection.prepareStatement("update t_goods set state=1 where  id=?");
            pstmt.setInt(1,id);
            int i=pstmt.executeUpdate();
            return i==1?1:0;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

//    添加商品
    @Override
    public int addGoods(Goods goods){
        try {
            pstmt = connection.prepareStatement("insert into t_goods(title, price, descr, img, state, mid) values(?, ?, ?, ?, ?, ?)");
            pstmt.setString(1, goods.getTitle());
            pstmt.setBigDecimal(2, goods.getPrice());
            pstmt.setString(3, goods.getDescr());
            pstmt.setString(4, goods.getImg());
            pstmt.setInt(5, 0);
            pstmt.setInt(6, goods.getMid());
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0; // 三目运算符
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

//    分页列表
    @Override
    public List<Goods> findAllGoods(int page,int size){
        List<Goods> list=new ArrayList<>();
        try{
            String sql = "SELECT id, title, price, descr, img, state, mid FROM (" +
                    "    SELECT " +
                    "        id, title, price, descr, img, state, mid, " +
                    "        ROW_NUMBER() OVER (ORDER BY id DESC) AS RowNum " +
                    "    FROM t_goods" +
                    ") AS T " +
                    "WHERE RowNum > ? AND RowNum <= ?";
            pstmt=connection.prepareStatement(sql);

            System.out.println(">LKLLlll"+(connection==null));
            System.out.println(">LKLLlll"+(pstmt==null));
            System.out.println((page-1)*size);
            System.out.println(size);
            pstmt.setInt(1, (page - 1) * size);
            pstmt.setInt(2, page*size);
            rset=pstmt.executeQuery();
           Goods goods=null;
           while(rset.next()){
               goods=new Goods(
                       rset.getInt("id"),
                       rset.getString("title"),
                       rset.getBigDecimal("price"),
                       rset.getString("descr"),
                       rset.getString("img"),
                       rset.getInt("state"),
                       rset.getInt("mid"));
               list.add(goods);
           }
           return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

}
